 /**
 * 逻辑、功能相关描述:
 *
 * @author yandawei 编写日期：2008-10-20
 */
package com.ce.pms.common.aop.impl;

import org.apache.log4j.Logger;

import com.ce.pms.common.annotation.AssociatedPrecinctQuery;
import com.ce.util.CurrentUserThread;

/**
 * @author yandawei
 *
 */
public aspect AssociatePrecinctSQLQueryAdvice extends
		AbstractAssociatePrecinctQueryAdvice {
	private Logger log =  Logger.getLogger(AssociatePrecinctSQLQueryAdvice.class);

	pointcut sessionCreateSQLQuery(String sql): execution(* createSQLQuery(String,..)) && args(sql) ;

	Object around(String sql , AssociatedPrecinctQuery associatedPrecinctQuery): sessionCreateSQLQuery(sql) && cflowbelowAssociatedPrecinctQuery(associatedPrecinctQuery){
		Long userId = CurrentUserThread.get().getUserId();
		log.info("===DACL===");
		String alias = associatedPrecinctQuery.alias();

		validateParameter("alias",alias);

		String beginStr = associatedPrecinctQuery.beginStr();

		validateParameter("beginStr",beginStr);

		String endStr = associatedPrecinctQuery.endStr();

		validateParameter("endStr",endStr);

		String insertAfterPoint = associatedPrecinctQuery.insertAfterPoint();

		validateParameter("insertAfterPoint",insertAfterPoint);

		String property4Precinct = associatedPrecinctQuery.property4Precinct();

		String tmp = String
		.format(
				" %1$s %2$s.%3$s IN (SELECT SEC_USER_ALIAS.RESOUCE_ID FROM SEC_USER_R_RIGTHS AS SEC_USER_ALIAS WHERE SEC_USER_ALIAS.TYPE_CODE ='001' AND SEC_USER_ALIAS.USER_ID  = '%4$s') %5$s",
				beginStr, alias, property4Precinct, userId, endStr);

		StringBuffer buffer = new StringBuffer(sql);

		buffer.insert(sql.lastIndexOf(insertAfterPoint) + insertAfterPoint.length(),tmp);

		return proceed(buffer.toString(),associatedPrecinctQuery);
	}
}
